var express = require('express');
var router = express.Router();
const mongoose = require('mongoose');
var { usersModel, roleModel, IOAModel, buildingsModel,
  floorModel, informationModel, room_typeModel, housemoneyModel, pingtaiModel,
  VIPModel, youhuiModel, xitongModel, rizhileiModel } = require("../../model/model")

/* GET home page. */



router.post('/jia', async (req, res) => {
  let users_list = await informationModel.create(req.body)
  res.send({
    code: 200
  })
});

router.post('/jia2', async (req, res) => {
  console.log(req.body);

  let users_list = await housemoneyModel.create(req.body)
  res.send({
    code: 200
  })
});



router.post('/jia3', async (req, res) => {
  console.log(req.body.dengji);

  // 检查 req.body.room_id 是否是一个数组
  if (Array.isArray(req.body.room_id)) {
    // 将字符串数组转换为 ObjectId 数组
    req.body.room_id = req.body.room_id.map(id => new mongoose.Types.ObjectId(id));
  }

  // 创建 VIPModel 实例
  let users_list = await VIPModel.create(req.body);
  res.send({
    code: 200
  })
});

router.post('/updateone', async (req, res) => {
  console.log(req.body);
  await informationModel.updateOne({ _id: req.body._id }, { $set: req.body })
  res.send({
    code: 200
  })
});
router.post('/updateone1', async (req, res) => {
  console.log(req.body);
  if (Array.isArray(req.body.room_id)) {
    // 将字符串数组转换为 ObjectId 数组
    req.body.room_id = req.body.room_id.map(id => new mongoose.Types.ObjectId(id));
  }
  await housemoneyModel.updateOne({ _id: req.body._id }, { $set: req.body })
  res.send({
    code: 200
  })
});


router.post('/updateone3', async (req, res) => {
  console.log(req.body);
  await VIPModel.updateOne({ _id: req.body._id }, { $set: req.body })
  res.send({
    code: 200
  })
});

router.post('/deleteone', async (req, res) => {
  console.log(req.body);
  await informationModel.deleteOne({ _id: req.body._id })
  res.send({
    code: 200
  })
});
router.post('/deleteone1', async (req, res) => {
  console.log(req.body);
  await housemoneyModel.deleteOne({ _id: req.body._id })
  res.send({
    code: 200
  })
});
router.post('/shuju', async (req, res) => {
  console.log(req.body);
  const page = req.body.page
  const pageSize = req.body.limit
  const offset = (page - 1) * pageSize;

  let data = await informationModel.find().populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize)
  let data1 = await buildingsModel.find()
  let data2 = await floorModel.find()
  let data3 = await room_typeModel.find()
  let data4 = await informationModel.find()
  res.send({
    code: 200,
    data,
    data1,
    data2,
    data3,
    data4
  })
});


router.post('/shuju1', async (req, res) => {
  console.log(req.body);
  const page = req.body.page
  const pageSize = req.body.limit
  const offset = (page - 1) * pageSize;

  let data = await housemoneyModel.find().populate(["room_id", "pingtaiid"]).skip(offset).limit(pageSize)
  let data2 = await pingtaiModel.find()
  let data3 = await room_typeModel.find()
  let data4 = await housemoneyModel.find()
  res.send({
    code: 200,
    data,
    data2,
    data3,
    data4
  })
});


router.post('/shuju2', async (req, res) => {
  console.log(req.body);
  const page = req.body.page
  const pageSize = req.body.limit
  const offset = (page - 1) * pageSize;

  let data = await VIPModel.find().populate(["youhuiid", "room_id"]).skip(offset).limit(pageSize)
  let data2 = await youhuiModel.find()
  let data3 = await room_typeModel.find()
  let data4 = await VIPModel.find()
  res.send({
    code: 200,
    data,
    data2,
    data3,
    data4

  })
});




router.post('/shuju3', async (req, res) => {
  const page = req.body.page
  const pageSize = req.body.limit
  const offset = (page - 1) * pageSize;

  let data = await xitongModel.find().populate(["rizhileiid"]).skip(offset).limit(pageSize)
  let data2 = await rizhileiModel.find()
  let data4 = await xitongModel.find()
  res.send({
    code: 200,
    data,
    data2,
    data4

  })
});






router.post('/chaxun', async (req, res) => {

  console.log(req.body);
  let tai = req.body.tai
  let loudong = req.body.loudong
  let loucheng = req.body.loucheng
  let fangxing = req.body.fangxing
  let fanghao = req.body.fanghao
  const page = req.body.page
  const pageSize = req.body.limit
  const offset = (page - 1) * pageSize;
  console.log(page, pageSize);
  console.log(req.body.fangxing);


  let data = []
  let length = 0

  if (tai) {
    data = await informationModel.find({ tai: tai }).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()
    length = data.length
    console.log(length);

  }
  if (tai && loudong) {
    data = await informationModel.find({ $and: [{ tai: tai }, { buildingsid: loudong }] }).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()
  }
  if (tai && loudong && loucheng) {
    data = await informationModel.find({
      $and: [{ tai: tai }, { buildingsid: loudong },
      { floorid: loucheng }]
    }).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()
  }

  if (tai && loudong && loucheng && fangxing) {
    data = await informationModel.find({
      $and: [{ tai: tai }, { buildingsid: loudong },
      { floorid: loucheng }, { room_id: fangxing }]
    }).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()
  }

  if (tai && loudong && loucheng && fangxing && fanghao) {
    data = await informationModel.find({
      $and: [{ tai: tai }, { buildingsid: loudong },
      { floorid: loucheng }, { room_id: fangxing }, { house: fanghao }]
    }).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()
  }

  if (loudong) {
    data = await informationModel.find(
      { buildingsid: loudong }).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()
  }

  if (loudong && loucheng) {
    data = await informationModel.find({
      $and: [{ buildingsid: loudong },
      { floorid: loucheng }]
    }).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()
  }


  if (loudong && loucheng && fangxing) {
    data = await informationModel.find({
      $and: [{ buildingsid: loudong },
      { floorid: loucheng }, { room_id: fangxing }]
    }).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()
  }
  if (loudong && loucheng && fangxing && fanghao) {
    data = await informationModel.find({
      $and: [{ buildingsid: loudong },
      { floorid: loucheng }, { room_id: fangxing }, { house: fanghao }]
    }).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()
  }

  if (loucheng) {
    data = await informationModel.find(
      { floorid: loucheng }).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()
  }

  if (loucheng && fangxing) {
    data = await informationModel.find({
      $and: [
        { floorid: loucheng }, { room_id: fangxing }]
    }).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()

  }


  if (loucheng && fangxing && fanghao) {
    data = await informationModel.find({
      $and: [
        { floorid: loucheng }, { room_id: fangxing }, { house: fanghao }]
    }).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()
  }


  if (fangxing) {
    data = await informationModel.find({ room_id: fangxing }).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()
  }
  if (fangxing && fanghao) {
    data = await informationModel.find({
      $and: [{ room_id: fangxing }, { house: fanghao }]
    }).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()
  }

  if (fanghao) {
    data = await informationModel.find({ house: fanghao }
    ).populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize).lean()
  }
  if (tai == "" && loudong == "" && loucheng == "" && fangxing == "" && fanghao == "") {
    data = await informationModel.find().populate(["buildingsid", "floorid", "room_id"]).skip(offset).limit(pageSize)
  }





  res.send({
    code: 200,
    data,
    length
  })
});




router.post('/chaxun2', async (req, res) => {


  let tai = req.body.tai
  let pingtaiid = req.body.pingtaiid
  let room_id = req.body.room_id
  let housename = req.body.housename
  const page = req.body.page
  const pageSize = req.body.limit
  const offset = (page - 1) * pageSize;
  console.log(page, pageSize);
  console.log(req.body.room_id, 111);

  let data = []
  if (tai) {
    data = await housemoneyModel.find({ tai: tai }).populate(["room_id", "pingtaiid"]).skip(offset).limit(pageSize).lean()
    length = data.length
    console.log(length);

  }
  if (tai && pingtaiid) {
    data = await housemoneyModel.find({ $and: [{ tai: tai }, { pingtaiid: pingtaiid }] }).populate(["room_id", "pingtaiid"]).skip(offset).limit(pageSize).lean()
  }
  if (tai && pingtaiid && room_id) {
    data = await housemoneyModel.find({
      $and: [{ tai: tai }, { pingtaiid: pingtaiid },
      { room_id: room_id }]
    }).populate(["room_id", "pingtaiid"]).skip(offset).limit(pageSize).lean()
  }

  if (tai && pingtaiid && room_id && housename) {
    data = await housemoneyModel.find({
      $and: [{ tai: tai }, { pingtaiid: pingtaiid },
      { room_id: room_id }, { housename: housename }]
    }).populate(["room_id", "pingtaiid"]).skip(offset).limit(pageSize).lean()
  }

  if (pingtaiid) {
    data = await housemoneyModel.find({ pingtaiid: pingtaiid }).populate(["room_id", "pingtaiid"]).skip(offset).limit(pageSize).lean()
  }

  if (pingtaiid && room_id) {
    data = await housemoneyModel.find({
      $and: [{ pingtaiid: pingtaiid },
      { room_id: room_id }]
    }).populate(["room_id", "pingtaiid"]).skip(offset).limit(pageSize).lean()
  }
  if (pingtaiid && room_id && housename) {
    data = await housemoneyModel.find({
      $and: [{ pingtaiid: pingtaiid },
      { room_id: room_id }, { housename: housename }]
    }).populate(["room_id", "pingtaiid"]).skip(offset).limit(pageSize).lean()
  }


  if (room_id) {
    data = await housemoneyModel.find(
      { room_id: room_id }).populate(["room_id", "pingtaiid"]).skip(offset).limit(pageSize).lean()
  }
  if (room_id && housename) {
    data = await housemoneyModel.find({
      $and: [
        { room_id: room_id }, { housename: housename }]
    }).populate(["room_id", "pingtaiid"]).skip(offset).limit(pageSize).lean()
  }

  if (housename) {
    data = await housemoneyModel.find({ housename: housename }).populate(["room_id", "pingtaiid"]).skip(offset).limit(pageSize).lean()
  }

  if (tai == "" && pingtaiid == "" && room_id == "" && housename == "") {
    data = await housemoneyModel.find().populate(["room_id", "pingtaiid"]).skip(offset).limit(pageSize)
  }






  res.send({
    code: 200,
    data

  })
});






router.post('/chaxun3', async (req, res) => {


  let tai = req.body.tai
  let youhuiid = req.body.youhuiid
  let vipname = req.body.vipname
  const page = req.body.page
  const pageSize = req.body.limit
  const offset = (page - 1) * pageSize;


  let data = []
  if (tai) {
    data = await VIPModel.find({ tai: tai }).populate(["room_id", "youhuiid"]).skip(offset).limit(pageSize).lean()
    length = data.length
    console.log(length);

  }
  if (tai && youhuiid) {
    data = await VIPModel.find({ $and: [{ tai: tai }, { youhuiid: youhuiid }] }).populate(["room_id", "youhuiid"]).skip(offset).limit(pageSize).lean()
  }
  if (tai && youhuiid && vipname) {
    data = await VIPModel.find({
      $and: [{ tai: tai }, { youhuiid: youhuiid },
      { vipname: vipname }]
    }).populate(["room_id", "youhuiid"]).skip(offset).limit(pageSize).lean()
  }

  if (youhuiid) {
    data = await VIPModel.find({ youhuiid: youhuiid }).populate(["room_id", "youhuiid"]).skip(offset).limit(pageSize).lean()
  }


  if (youhuiid && vipname) {
    data = await VIPModel.find({
      $and: [{ youhuiid: youhuiid },
      { vipname: vipname }]
    }).populate(["room_id", "youhuiid"]).skip(offset).limit(pageSize).lean()
  }

  if (vipname) {
    data = await VIPModel.find(
      { vipname: vipname }).populate(["room_id", "youhuiid"]).skip(offset).limit(pageSize).lean()
  }



  if (tai == "" && youhuiid == "" && vipname == "") {
    data = await VIPModel.find().populate(["room_id", "youhuiid"]).skip(offset).limit(pageSize)
  }






  res.send({
    code: 200,
    data

  })
});





router.post('/chaxun4', async (req, res) => {


  let rizhileiid = req.body.rizhileiid
  const startDate = String(new Date(req.body.startDate));
  const endDate = String(new Date(req.body.endDate));
  const page = req.body.page
  const pageSize = req.body.limit
  const offset = (page - 1) * pageSize;
  // endDate.setMilliseconds(endDate.getMilliseconds() + 1);




  let data = []
  if (rizhileiid) {
    data = await xitongModel.find({ rizhileiid: rizhileiid }).populate(["rizhileiid"]).skip(offset).limit(pageSize).lean()
    length = data.length
    console.log(length);

  }
  if (startDate != "Invalid Date" && endDate != "Invalid Date") {
    console.log(startDate, endDate);
    data = await xitongModel.find({
      time: { $gte: req.body.startDate, $lt: req.body.endDate }
    }).populate(["rizhileiid"]).skip(offset).limit(pageSize)
  }


  if (rizhileiid && startDate != "Invalid Date" && endDate != "Invalid Date") {
    data = await xitongModel.find({
      $and: [{ rizhileiid: rizhileiid },
      {
        time: { $gte: req.body.startDate, $lt: req.body.endDate }
      }
      ]
    }).populate(["rizhileiid"]).skip(offset).limit(pageSize)
  }



  if (rizhileiid == "" && startDate == "Invalid Date" && endDate == "Invalid Date") {
    console.log(1);

    data = await xitongModel.find().populate(["rizhileiid"]).skip(offset).limit(pageSize)
  }
  res.send({
    code: 200,
    data

  })
});









router.get('/users_list', async (req, res) => {
  let users_list = await usersModel.find();
  res.send({
    users_list
  })
});

router.get('/role_list', async (req, res) => {
  let role_list = await roleModel.find();
  res.send({
    role_list
  })
});

router.get('/IOA_list', async (req, res) => {
  let IOAlist = await IOAModel.find().lean()
  let cateObj = {}
  IOAlist.forEach(item => {
    cateObj[item._id] = item
  })

  let data = []
  IOAlist.forEach((item) => {
    if (!item["Iid"]) {
      data.push(item)
    } else {
      if (cateObj[item["Iid"]] && !cateObj[item["Iid"]]["children"]) {
        cateObj[item["Iid"]]["children"] = []

      }
      if (cateObj[item["Iid"]]) {
        cateObj[item["Iid"]]["children"].push(item)
      }
    }
  })
  res.send({
    code: 200,
    data
  })
})



module.exports = router;
